﻿/*
 * Author: Alexander Sjösten
 * Thomas Mattsson wrote the ToString-method
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Final_Project.SessionData
{
    /// <summary>
    /// This class represents one Treatment session in the program.
    /// It stores information about date, price and textual description
    /// of the session. It also holds the amount of minutes for the session.
    /// </summary>
    
    [Serializable()]
    public class TreatmentSession : ISerializable
    {
        // Local variables
        private double price;
        private DateTime date;
        private string description;
        private int time;

        /// <summary>
        /// Constructor for the TreatmentSession taking 0 parameters,
        /// will chain call next constructor
        /// </summary>
        public TreatmentSession()
            : this(0.0)
        { }

        /// <summary>
        /// Constructor for the TreatmentSession taking 1 parameter,
        /// will chain call next constructor
        /// </summary>
        /// <param name="price">The price of the session</param>
        public TreatmentSession(double price)
            : this(price, new DateTime())
        { }

        /// <summary>
        /// Constructor for the TreatmentSession taking 2 parameters,
        /// will chain call next constructor
        /// </summary>
        /// <param name="price">The price of the session</param>
        /// <param name="date">The date of the session</param>
        public TreatmentSession(double price, DateTime date)
            : this(price, date, string.Empty)
        { }

        /// <summary>
        /// Constructor for the TreatmentSession taking 3 parameters,
        /// will chain call next constructor
        /// </summary>
        /// <param name="price">The price of the session</param>
        /// <param name="date">The date of the session</param>
        /// <param name="description">The description of the session</param>
        public TreatmentSession(double price, DateTime date, string description)
            : this(price, date, description, 0)
        { }

        /// <summary>
        /// Constructor for the TreatmentSession taking 4 parameters
        /// </summary>
        /// <param name="price">The price of the session</param>
        /// <param name="date">The date of the session</param>
        /// <param name="description">The discription of the session</param>
        /// <param name="time">The amount of time the session will take</param>
        public TreatmentSession(double price, DateTime date, string description, int time)
        {
            this.price = price;
            this.date = date;
            this.description = description;
            this.time = time;
        }

        /// <summary>
        /// Copy constructor for the TreatmentSession class
        /// </summary>
        /// <param name="session">TreatmentSession to copy from</param>
        public TreatmentSession(TreatmentSession session)
        {
            price = session.Price;
            date = session.Date;
            description = session.Description;
            time = session.Time;
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">The information to the object from the deserialization</param>
        /// <param name="ctxt">The streaming context</param>
        public TreatmentSession(SerializationInfo info, StreamingContext ctxt)
        {
            price = (double)info.GetValue("PriceInfo", typeof(double));
            date = (DateTime)info.GetValue("DateInfo", typeof(DateTime));
            description = (String)info.GetValue("DescriptionInfo", typeof(string));
            time = (int)info.GetValue("TimeInfo", typeof(int));
        }

        /// <summary>
        /// Property for the price
        /// </summary>
        public double Price {
            get { return price; }
            set 
            {
                if (value > 0)
                {
                    price = value;
                }
            }
        }

        /// <summary>
        /// Property for the date
        /// </summary>
        public DateTime Date
        {
            get { return DateTime.Now; }
            set 
            {
                if (value != null)
                {
                    date = value;
                }
            }
        }

        /// <summary>
        /// Property for the description
        /// </summary>
        public string Description
        {
            get { return description; }
            set 
            {
                if (!string.IsNullOrEmpty(value))
                {
                    description = value;
                }
            }
        }

        /// <summary>
        /// Property for the time
        /// </summary>
        public int Time
        {
            get { return time; }
            set 
            {
                if (value > 0)
                {
                    time = value;
                }
            }
        }


        /// <summary>
        /// This method creates a textual representation of this 
        /// treatment session
        /// </summary>
        /// <returns>The formatted string of this session</returns>
        public override string ToString()
        {
            int descriptionLength = (Description.Length > 33) ? 33 : Description.Length;
            return string.Format("{0,-10} {1,-36} {2,-3}  {3:C}",
                Date.ToShortDateString(), Description.Substring(0, descriptionLength), Time.ToString(), Price);
        }

        /// <summary>
        /// Method for serialization, must be added since it's
        /// part of the ISerializable interface
        /// </summary>
        /// <param name="info">The serialization info</param>
        /// <param name="ctxt">The streaming context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("PriceInfo", Price);
            info.AddValue("DateInfo", Date);
            info.AddValue("DescriptionInfo", Description);
            info.AddValue("TimeInfo", Time);
        }
    }
}
